package com.uminho.objects;

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.util.ArrayList;

import com.uminho.http.Webservice;

import android.graphics.drawable.Drawable;
import android.util.Log;

/****c* Objects/User
* DESCRIPTION
* Objeto User que representa um utilizador do egend.me.
****
*/
public class User extends Profile {
 
	ArrayList<Service> bookmark;
	ArrayList<Appointment> appointments;

	/****f* User/User
	* DESCRIPTION 
	* cria um objeto User.
	* INPUTS
	* Recebe o identificador do utilizador, o username, o nome, o email associado, o número de telefone e telemóvel, o site, o género e a morada
	* RESULT 
	* Retorna um objeto do tipo User
	* EXAMPLE
	User(1321321, "jo", "João Oliveira", "materiais@m.pt",
  "1232131245124", "3123123123", "http://m.pt", 'm',
  "Rua direita, Braga, Portugal");
	****
	*/
	public User(Long id, String username, String name, String email, String phone, String mobile_phone,
			String website, Character gender, String address){
		super(id,username, name, email,phone,mobile_phone,website,gender,address);
	}
	
	public User(String id, String name, String address) {
		this.id = Long.parseLong(id);
		this.name = name;
		this.address =address;
	}
	
	public User(String name, String address) {
		this.name = name;
		this.address =address;
	}
	
	public User() {
	}
	
	public ArrayList<Appointment> getAppointments() {
		return appointments;
	}

	public void setAppointments(ArrayList<Appointment> appointments) {
		this.appointments = appointments;
	}

	public void addAppointment(Appointment a) {
		if (!this.appointments.contains(a)) {
			this.appointments.add(a);
		}
	}

	public void remAppointment(Appointment a) {
		this.appointments.remove(a);
	}
	
	public ArrayList<Service> getBookmark() {
		return bookmark;
	}

	public void setBookmark(ArrayList<Service> bookmark) {
		this.bookmark = bookmark;
	}

	public void addBookmark(Service s) {
		if (!this.bookmark.contains(s)) {
			this.bookmark.add(s);
		}
	}

	public void remBookmark(Service s) {
		this.bookmark.remove(s);
	}

	/****f* User/setAvatar
	* DESCRIPTION 
	* Define o avatar do objeto User. Esse avatar é uma imagem hospedada no servidor do Egend.me, por isso este método não recebe nenhum argumento.
	* RESULT 
	* É definido o avatar para o objeto associado e pronto a ser utilizado pela aplicação Android
	* EXAMPLE
	Webservice w = Webservice.getInstance();
	User u = w.getProfile();
	u.setAvatar();
	****
	*/
	@Override
	public void setAvatar() throws MalformedURLException, IOException {
		String urlAvatar = Webservice.domain + "/avatars/user?id=" + this.getId().toString() + "&res=" + displaySize();
		Log.d("****", urlAvatar);
		InputStream is = fetch(urlAvatar);
		Drawable drawable = Drawable.createFromStream(is, "src");
		this.avatar = drawable;
	}
}
